<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Category;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class CategoryController extends CommonController
{
    //get admin/category   全部分类列表
    public function index(){
//        $category = Category::all();
//        dd($category);
        $category = Category::tree();
        return view('admin.category.index')->with('data',$category);
    }

    

    //post admin/category
    public function store(){
        $input = Input::except("_token");
//        dd($input);
        //验证
        $rules = [
            'cate_name'=>'required',//定义规则，规则都是框架自带的
        ];
        //提示信息
        $message = [
            'cate_name.required'=>'分类名称不能为空',

        ];
        //创建验证对象，哪些post值，规则是什么，提示什么

        $validator = Validator::make($input,$rules,$message);
        //如果通过验证，
        if($validator->passes()){

            $re = Category::create($input);
            if($re){
                return redirect('admin/category');
            }else{
                return back()->with('errors','数据填入失败');
            }


        }else{

            return back()->withErrors($validator);
        }

    }

    //get admin/category/create   添加页面的显示及数据分配
    public function create(){
        $cate_datas = Category::where("cate_pid","0")->get();
        return view('admin.category.add',compact('cate_datas'));
    }

    //put admin/category/{category}
    public function update($cate_id){
        $input = Input::except('_token','_method');
        $re = Category::where("cate_id",$cate_id)->update($input);
        if($re){
            return redirect('admin/category');
        }else{
            return back()->with('msg','分类更新失败');
        }

    }

    //get admin/category/{category}/edit   编辑页面的显示及数据分配
    public function edit($cate_id){
        $cate_datas = Category::where("cate_pid","0")->get();
        $cate_data = Category::find($cate_id);
        return view('admin.category.edit',compact('cate_datas','cate_data'));
    }

    
    //delete admin/category/{category} 
    public function destroy($cate_id){
        $re = Category::where("cate_id",$cate_id)->delete();
        Category::where("cate_pid",$cate_id)->update(['cate_pid'=>0]);
        if($re){
            $data = [
                'status'=>1,
                'msg'=>"分类删除成功！"
            ];
        }else{
            $data = [
                'status'=>0,
                'msg'=>"分类删除失败"
            ];
        }
        return $data;
    }


    //get admin/category/{category}
    public function show(){

    }




    
    
    public function change_order(){
        $input = Input::all();
//        dd($input);
        $cate_order = $input['cate_order'];
        $cate_data = Category::find($input['cate_id']);
        $cate_data->cate_order = $cate_order;
        $re = $cate_data->update();
        if($re){
           $data=[
               'status'=>'1',
               'msg'=>"排序修改成功"
           ];
        }else{
            $data=[
                'status'=>'0',
                'msg'=>"排序修改失败"
            ];
        }

        return $data;
    }

}
